package com.ysd.lis.service.bac.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacSiteOfInfectionDict;
import com.ysd.lis.entity.sta.StaInfectSite;
import com.ysd.lis.mapper.bac.BacSiteOfInfectionDictMapper;
import com.ysd.lis.request.bac.BacGermBaseDictRequest;
import com.ysd.lis.service.bac.BacSiteOfInfectionDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * @author LuoXS
 * @date 2024/3/27
 * version 1.0
 */
@Service
public class BacSiteOfInfectionDictServiceImpl extends ServiceImpl<BacSiteOfInfectionDictMapper, BacSiteOfInfectionDict> implements BacSiteOfInfectionDictService {


    @Autowired
    BacSiteOfInfectionDictMapper mapper;

    @Override
    public Result add(BacSiteOfInfectionDict bacSiteOfInfectionDict) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                bacSiteOfInfectionDict.setCreator(sysUser.getCreator());
                bacSiteOfInfectionDict.setOrgId(sysUser.getOrgId());
            }
            boolean save = this.save(bacSiteOfInfectionDict);
            if (!save) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDit(BacSiteOfInfectionDict bacSiteOfInfectionDict) {
        boolean b = this.updateById(bacSiteOfInfectionDict);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result pageList(BacGermBaseDictRequest.BacSiteOfInfectionDictPageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<BacSiteOfInfectionDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            Page<BacSiteOfInfectionDict> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());
            // 参数
            if (!StringUtils.isEmpty(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(wrapper ->
                        wrapper.like(BacSiteOfInfectionDict::getName, pageParam.getSearchValue())
                                .or().like(BacSiteOfInfectionDict::getStandardName, pageParam.getSearchValue()));

            }
            lambdaQueryWrapper.eq(BacSiteOfInfectionDict::getDelFlag, 0);
            lambdaQueryWrapper.orderByAsc(BacSiteOfInfectionDict::getDisplayOrder);
            Page<BacSiteOfInfectionDict> labInstrSetupPage = mapper.selectPage(page, lambdaQueryWrapper);
            result = Result.succ(labInstrSetupPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result delete(BacGermBaseDictRequest.BacSiteOfInfectionDictDeleteParam deleteParam) {
        Result result;

        try {
            if (ToolsUtils.isEmpty(deleteParam) || ToolsUtils.isEmpty(deleteParam.getIds())) {
                return Result.fail("入参不可为空！");
            }
            String[] array = deleteParam.getIds().split(",");
            boolean b = this.removeByIds(Arrays.asList(array));
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result findDictList(BacSiteOfInfectionDict pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<BacSiteOfInfectionDict> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseEntity::getDelFlag, false)
                    .isNotNull(BacSiteOfInfectionDict::getName)
                    .isNotNull(BacSiteOfInfectionDict::getCode);

            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                wrapper.eq(BacSiteOfInfectionDict::getOrgId, sysUser.getOrgId());
            }
            if (StringUtils.isNotEmpty(pageParam.getFilter())) {
                wrapper.and(query -> query.like(BacSiteOfInfectionDict::getCode, pageParam.getFilter()).or().like(BacSiteOfInfectionDict::getName, pageParam.getFilter()));
            }
            List<BacSiteOfInfectionDict> dicts = mapper.selectList(wrapper);
            result = Result.succ(dicts);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result staDataSiteOfInfectionDict(List<StaInfectSite> staInfectSites) {
        for (StaInfectSite staResistantType : staInfectSites) {
            BacSiteOfInfectionDict siteOfInfectionDict = new BacSiteOfInfectionDict();
            BeanUtils.copyProperties(staResistantType, siteOfInfectionDict, "id");
            siteOfInfectionDict.setCode(staResistantType.getInfectSiteNo());
            siteOfInfectionDict.setName(staResistantType.getInfectSiteNa());
            siteOfInfectionDict.setStandardName(staResistantType.getStandardNa());
            siteOfInfectionDict.setDisplayOrder(staResistantType.getSeq());
            this.saveOrUpdate(siteOfInfectionDict, new LambdaQueryWrapper<BacSiteOfInfectionDict>().eq(BacSiteOfInfectionDict::getCode, siteOfInfectionDict.getCode()).eq(BacSiteOfInfectionDict::getDelFlag, 0));
        }
        return Result.succ(1, "同步成功！", null);
    }
}
